package com.qiankun.tang.ui.view;

import android.content.Context;
import android.support.annotation.IdRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.qiankun.tang.R;


/**
 * @author txf
 * @Title
 * @package com.tang.test
 * @date 2017/5/11 0011
 */

public class TabBar extends LinearLayout implements View.OnClickListener {
    View tab1_icon, tab2_icon, tab3_icon, tab4_icon, tab5_icon;
    View tab1_tv,tab2_tv,tab3_tv,tab4_tv;
    public TabBar(Context context) {
        super(context);
        init();
    }

    public TabBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TabBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setHideTabPos(int pos) {
        switch (pos){
            case 0:
                findViewById(R.id.tab1).setVisibility(GONE);
                break;
            case 1:
                findViewById(R.id.tab2).setVisibility(GONE);
                break;
            case 2:
                findViewById(R.id.tab3).setVisibility(GONE);
                break;
            case 3:
                findViewById(R.id.tab4).setVisibility(GONE);
                break;
        }
    }
    public void setShowTabPos(int pos) {
        switch (pos){
            case 0:
                findViewById(R.id.tab1).setVisibility(VISIBLE);
                break;
            case 1:
                findViewById(R.id.tab2).setVisibility(VISIBLE);
                break;
            case 2:
                findViewById(R.id.tab3).setVisibility(VISIBLE);
                break;
            case 3:
                findViewById(R.id.tab4).setVisibility(VISIBLE);
                break;
        }
    }

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_tabbar, this);
        tab1_icon = findViewById(R.id.tab1_icon);
        tab2_icon = findViewById(R.id.tab2_icon);
        tab3_icon = findViewById(R.id.tab3_icon);
        tab4_icon = findViewById(R.id.tab4_icon);

        tab1_tv = findViewById(R.id.tab1_tv);
        tab2_tv = findViewById(R.id.tab2_tv);
        tab3_tv = findViewById(R.id.tab3_tv);
        tab4_tv = findViewById(R.id.tab4_tv);

        findViewById(R.id.tab1).setOnClickListener(this);
        findViewById(R.id.tab2).setOnClickListener(this);
        findViewById(R.id.tab3).setOnClickListener(this);
        findViewById(R.id.tab4).setOnClickListener(this);

        tab1_icon.setSelected(true);
        tab1_tv.setSelected(true);
    }
    public View getTab1() {
        return   findViewById(R.id.tab1);
    }
    OnTabCheckedChangeListener l;
    public void setOnTabCheckedChangeListener(OnTabCheckedChangeListener l) {
        this.l = l;
    }
    public interface OnTabCheckedChangeListener {
         void onCheckedChanged(@IdRes int checkedId);
    }
    @Override
    public void onClick(View v) {
        tab1_icon.setSelected(false);
        tab2_icon.setSelected(false);
        tab3_icon.setSelected(false);
        tab4_icon.setSelected(false);

        tab1_tv.setSelected(false);
        tab2_tv.setSelected(false);
        tab3_tv.setSelected(false);
        tab4_tv.setSelected(false);
        switch (v.getId()) {
            case R.id.tab1:
                if (!tab1_icon.isSelected()) {
                    tab1_icon.setSelected(true);
                    tab1_tv.setSelected(true);
                    if (l != null)
                        l.onCheckedChanged(v.getId());
                }
                break;
            case R.id.tab2:
                if (!tab2_icon.isSelected()) {
                    tab2_icon.setSelected(true);
                    tab2_tv.setSelected(true);
                    if (l != null)
                        l.onCheckedChanged(v.getId());
                }
                break;
            case R.id.tab3:
                if (!tab3_icon.isSelected()) {
                    tab3_icon.setSelected(true);
                    tab3_tv.setSelected(true);
                    if (l != null)
                        l.onCheckedChanged(v.getId());
                }
                break;
            case R.id.tab4:
                if (!tab4_icon.isSelected()) {
                    tab4_icon.setSelected(true);
                    tab4_tv.setSelected(true);
                    if (l != null)
                        l.onCheckedChanged(v.getId());
                }
                break;
        }
    }
}
